﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace client.app.gui2
{
    /// <summary>
    /// HeroListUI.xaml 的交互逻辑
    /// </summary>
    public partial class HeroListUI : UserControl
    {
        public HeroListUI()
        {
            InitializeComponent();

            tbNickname.Text = comm.GUID.NewString();
            btnCreate.Click += new RoutedEventHandler(btnCreate_Click);

            FillStar();
            FillHeroType();
            FillMyHero();
        }

        void btnJoin_Click(object sender, RoutedEventArgs e)
        {
            var btn = (Button1)sender;
            var ss = (string[])btn.Tag;
            var userId = ss[0];
            var playerId = ss[1];
            TcpClient.Call<comm.objst.MessageResult>(comm.objst.TcpMessageType.ToServerHeroJoinWorld, playerId, (result, message) =>
            {
                Console.WriteLine("JoinWorld:" + result.Message);
                if (result.IsSucc)
                {
                    MainGame.MainSpace.JoinWorld(userId, playerId);
                }
            });
        }
        void btnCreate_Click(object sender, RoutedEventArgs e)
        {
            string starId = Selected(wpStar),
                heroId = Selected(wpHeroType),
                facId = Selected(wpFaction);
            if (starId != null && heroId != null && facId != null)
            {
                tbNickname.Text = tbNickname.Text.Trim();
                if (tbNickname.Text.Length != 0)
                {
                    var createHero = new comm.objst.CreateHero
                    {
                        UserId = Conf.USER_ID,
                        StarId = starId,
                        FactionId = facId,
                        HeroId = heroId,
                        Nickname = tbNickname.Text
                    };
                    TcpClient.Call<comm.objst.MessageResult>(comm.objst.TcpMessageType.ToServerCreateHero, createHero, (result, message) =>
                    {
                        Dispatcher.BeginInvoke(new Action(() =>
                        {
                            tbNickname.Text = comm.GUID.NewString();
                        }));
                        Console.WriteLine("CreateHero:" + result.Message);
                        FillMyHero();
                    });
                }
            }
        }

        string Selected(Panel panel)
        {
            foreach (var item in panel.Children)
            {
                var ck = (RadioButton1)item;
                if (ck.IsChecked)
                {
                    return ck.Tag.ToString();
                }
            }
            return null;
        }

        void FillStar()
        {
            TcpClient.Call<comm.objst.StarInfo[]>(comm.objst.TcpMessageType.ToServerGetStarList, null, (stars, message) =>
            {
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    wpStar.Children.Clear();
                    RadioButton1 first = null;
                    foreach (var star in stars)
                    {
                        var star2 = star;
                        var radio = new RadioButton1
                        {
                            GroupName = "star",
                            Tag = star.StarId,
                            Foreground = new SolidColorBrush(Color2.White()),
                            Text = star.StarId + "(" + star.CanCreateHeroByPlayer + ")",
                            Margin = new Thickness(0, 0, 4, 4)
                        };
                        radio.Checked += new RoutedEventHandler((o, e) =>
                        {
                            FillFaction(star2.StarId);
                        });
                        if (first == null)
                        {
                            first = radio;
                        }
                        wpStar.Children.Add(radio);
                    }
                    if (first != null)
                    {
                        first.IsChecked = true;
                    }
                }));
            });
        }
        void FillFaction(string starId)
        {
            TcpClient.Call<comm.objst.FactionInfo[]>(comm.objst.TcpMessageType.ToServerGetStarFactionList, starId, (facs, message) =>
            {
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    wpFaction.Children.Clear();
                    RadioButton1 first = null;
                    foreach (var fac in facs)
                    {
                        var radio = new RadioButton1
                        {
                            GroupName = "fac",
                            Tag = fac.FactionId,
                            Foreground = new SolidColorBrush(Color2.White()),
                            Text = fac.FactionId,
                            Margin = new Thickness(0, 0, 4, 4)
                        };
                        if (first == null)
                        {
                            first = radio;
                        }
                        wpFaction.Children.Add(radio);
                    }
                    if (first != null)
                    {
                        first.IsChecked = true;
                    }
                }));
            });
        }
        void FillHeroType()
        {
            TcpClient.Call<string[]>(comm.objst.TcpMessageType.ToServerGetHeroTypeList, null, (heroIds, message) =>
            {
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    wpHeroType.Children.Clear();
                    RadioButton1 first = null;
                    foreach (var heroId in heroIds)
                    {
                        var radio = new RadioButton1
                        {
                            GroupName = "hero",
                            Tag = heroId,
                            Foreground = new SolidColorBrush(Color2.White()),
                            Text = heroId,
                            Margin = new Thickness(0, 0, 4, 4)
                        };
                        if (first == null)
                        {
                            first = radio;
                        }
                        wpHeroType.Children.Add(radio);
                    }
                    if (first != null)
                    {
                        first.IsChecked = true;
                    }
                }));
            });
        }
        void FillMyHero()
        {
            TcpClient.Call<comm.objst.HeroCreateInfo[]>(comm.objst.TcpMessageType.ToServerGetMyHeroList, Conf.USER_ID, (heros, message) =>
            {
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    spHero.Children.Clear();
                    foreach (var hero in heros)
                    {
                        var grid = new Grid { Margin = new Thickness(0, 0, 0, 4) };
                        var tbName = new TextBlock
                        {
                            Margin = new Thickness(0, 0, 60, 0),
                            VerticalAlignment = VerticalAlignment.Center
                        };
                        tbName.Inlines.Add(new TextBlock
                        {
                            Text = hero.Nickname,
                            Foreground = new SolidColorBrush(Color2.White(50)),
                            Width = 80
                        });
                        tbName.Inlines.Add(new TextBlock
                        {
                            Text = " online:" + hero.IsOnline,
                            Foreground = new SolidColorBrush(hero.IsOnline ? Color2.White(20) : Color2.Yellow()),
                        });
                        grid.Children.Add(tbName);
                        var btnJoin = new Button1
                        {
                            Tag = new string[] { hero.UserId, hero.PlayerId },
                            Padding = new Thickness(2),
                            Text = "进入游戏",
                            IsEnabled = !hero.IsOnline,
                            Foreground = new SolidColorBrush(Color2.White()),
                            HorizontalAlignment = HorizontalAlignment.Right,
                            VerticalAlignment = VerticalAlignment.Center
                        };
                        btnJoin.Click += new RoutedEventHandler(btnJoin_Click);
                        grid.Children.Add(btnJoin);

                        spHero.Children.Add(grid);
                    }


                    this.UpBeginDraw();
                }));
            });
        }
    }
}
